﻿#include "trianglewindow.h"
#include <QDebug>

static const char *vertexShaderSource =
    "attribute highp vec4 posAttr;\n"
    "attribute lowp vec4 colAttr;\n"
    "varying lowp vec4 col;\n"
    "uniform highp mat4 matrix;\n"
    "void main() {\n"
    "   col = colAttr;\n"
    "   gl_Position = matrix * posAttr;\n"
    "}\n";

static const char *fragmentShaderSource =
    "varying lowp vec4 col;\n"
    "void main() {\n"
    "   gl_FragColor = col;\n"
    "}\n";

TriangleWindow::TriangleWindow()
    :m_program(0)
    ,m_frame(0)
{

}

TriangleWindow::~TriangleWindow()
{

}

void TriangleWindow::initialize()
{
    //创建着色器程序
    m_program = new QOpenGLShaderProgram(this);
    //编译代码-添加顶点着色器
    m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
    //编译代码-添加片段着色器
    m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);

    //链接着色器
    m_program->link();

    //得到顶点属性名在着色器参数列表中的位置-0
    m_posAttr = m_program->attributeLocation("posAttr");
    //得到颜色属性名在着色器参数列表中的位置-1
    m_colAttr = m_program->attributeLocation("colAttr");
    //得到矩阵规格名在着色器参数列表中的位置-0
    m_matrixUniform = m_program->uniformLocation("matrix");
}

void TriangleWindow::render()
{
    //分辨率比例-1
    const qreal retinaScale = devicePixelRatio();

    //重置视口
    glViewport(0, 0, width() * retinaScale, height() * retinaScale);

    //清除颜色缓冲区
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnable(GL_DEPTH_TEST);

    //绑定着色器程序到活动的上下文
    m_program->bind();

    //4x4 矩阵
    QMatrix4x4 matrix;
    //乘以一个矩阵，为了建立透视投影矩阵
    matrix.perspective(60.0f, 4.0f/3.0f, 0.1f, 100.0f);
    //乘以这个矩阵，通过向量转换坐标
    matrix.translate(0, 0, -4);
    //乘以这个矩阵，通过向量上的角度旋转坐标
    matrix.rotate(60*-45 / screen()->refreshRate(), 1, 0, 0);
//    matrix.rotate(30, 1,0,0);

    //设置矩阵数据
    m_program->setUniformValue(m_matrixUniform, matrix);

    //顶点坐标
    GLfloat vertices[] = {
        //正面
        -0.5f, 0.0f, 0.5f,     //1
        0.5f, 0.0f, 0.5f,      //2
        0.5f, 1.0f, 0.5f,      //3
        -0.5f, 1.0f, 0.5f,     //4

        //右面
        0.5f, 0.0f, 0.5f,       //2
        0.5f, 0.0f, -0.5f,      //5
        0.5f, 1.0f, -0.5f,      //6
        0.5f, 1.0f, 0.5f,       //3

        //左面
        -0.5f, 0.0f, 0.5f,      //1
        -0.5f, 0.0f, -0.5f,     //8
        -0.5f, 1.0f, -0.5f,     //7
        -0.5f, 1.0f, 0.5f,      //4

        //背面
        0.5f, 0.0f, -0.5f,      //5
        0.5f, 1.0f, -0.5f,      //6
        -0.5f, 1.0f, -0.5f,     //7
        -0.5f, 0.0f, -0.5f,     //8

        //顶面
        0.5f, 1.0f, 0.5f,       //3
        0.5f, 1.0f, -0.5f,      //6
        -0.5f, 1.0f, -0.5f,     //7
        -0.5f, 1.0f, 0.5f,      //4

        //底面
        0.5f, 0.0f, 0.5f,       //2
        0.5f, 0.0f, -0.5f,      //5
        -0.5f, 0.0f, -0.5f,     //8
        -0.5f, 0.0f, 0.5f,      //1

    };

    //颜色值
    GLfloat colors[] = {
        //正面
        1.0f, 0.0f, 0.0f,   //1 红
        0.0f, 1.0f, 0.0f,   //2 绿
        0.0f, 0.0f, 1.0f,   //3 蓝
        1.0f, 1.0f, 1.0f,   //4 白

        //右面
        0.0f, 1.0f, 0.0f,   //2 绿
        1.0f, 1.0f, 0.0f,   //5 黄
        0.0f, 1.0f, 1.0f,   //6 青
        0.0f, 0.0f, 1.0f,   //3 蓝

        //左面
        1.0f, 0.0f, 0.0f,   //1 红
        1.0f, 0.0f, 1.0f,   //8 紫
        1.0f, 0.6f, 0.0f,   //7 橘
        1.0f, 1.0f, 1.0f,   //4 白

        //背面
        1.0f, 1.0f, 0.0f,   //5 黄
        0.0f, 1.0f, 1.0f,   //6 青
        1.0f, 0.6f, 0.0f,   //7 橘
        1.0f, 0.0f, 1.0f,   //8 紫

        //顶面
        0.0f, 0.0f, 1.0f,   //3 蓝
        0.0f, 1.0f, 1.0f,   //6 青
        1.0f, 0.6f, 0.0f,   //7 橘
        1.0f, 1.0f, 1.0f,   //4 白

        //底面
        0.0f, 1.0f, 0.0f,   //2 绿
        1.0f, 1.0f, 0.0f,   //5 黄
        1.0f, 0.0f, 1.0f,   //8 紫
        1.0f, 0.0f, 0.0f,   //1 红

    };

    //glVertexAttribPointer 指定了渲染时索引值为 index 的顶点属性数组的数据格式和位置
    //设置顶点数据
    glVertexAttribPointer(m_posAttr, 3, GL_FLOAT, GL_FALSE, 0, vertices);
    //设置颜色数据
    glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors);

    //启用对应的顶点属性数组
    //顶点坐标
    glEnableVertexAttribArray(0);
    //颜色值
    glEnableVertexAttribArray(1);

    //绘制三角形
    glDrawArrays(GL_QUADS, 0, 24);

    //停用对应的顶点属性数组
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);

    //释放程序
    m_program->release();

    m_frame++;

}
